package com.dianping.cat.consumer.business.model.transform;

import com.dianping.cat.consumer.business.model.IVisitor;
import com.dianping.cat.consumer.business.model.entity.BusinessItem;
import com.dianping.cat.consumer.business.model.entity.BusinessReport;
import com.dianping.cat.consumer.business.model.entity.Segment;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(BusinessReport businessReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(businessReport, out);
    return out.toByteArray();
  }


  public static void build(BusinessReport businessReport, OutputStream out) { businessReport.accept(new DefaultNativeBuilder(out)); }



  public void visitBusinessItem(BusinessItem businessItem) {
/*  41 */     if (businessItem.getId() != null) {
/*  42 */       writeTag(1, 1);
/*  43 */       writeString(businessItem.getId());
    }

/*  46 */     if (businessItem.getType() != null) {
/*  47 */       writeTag(2, 1);
/*  48 */       writeString(businessItem.getType());
    }

/*  51 */     if (!businessItem.getSegments().isEmpty()) {
/*  52 */       writeTag(33, 2);
/*  53 */       writeInt(businessItem.getSegments().size());

/*  55 */       for (Segment segment : businessItem.getSegments().values()) {
/*  56 */         segment.accept(this.m_visitor);
      }
    }

/*  60 */     writeTag(63, 3);
  }


  public void visitBusinessReport(BusinessReport businessReport) {
/*  65 */     writeTag(63, 0);

/*  67 */     if (businessReport.getDomain() != null) {
/*  68 */       writeTag(1, 1);
/*  69 */       writeString(businessReport.getDomain());
    }

/*  72 */     if (businessReport.getStartTime() != null) {
/*  73 */       writeTag(2, 1);
/*  74 */       writeDate(businessReport.getStartTime());
    }

/*  77 */     if (businessReport.getEndTime() != null) {
      writeTag(3, 1);
/*  79 */       writeDate(businessReport.getEndTime());
    }

/*  82 */     if (!businessReport.getBusinessItems().isEmpty()) {
/*  83 */       writeTag(33, 2);
/*  84 */       writeInt(businessReport.getBusinessItems().size());

/*  86 */       for (BusinessItem businessItem : businessReport.getBusinessItems().values()) {
/*  87 */         businessItem.accept(this.m_visitor);
      }
    }

/*  91 */     writeTag(63, 3);
  }


  public void visitSegment(Segment segment) {
/*  96 */     if (segment.getId() != null) {
/*  97 */       writeTag(1, 1);
/*  98 */       writeInt(segment.getId().intValue());
    }

/* 101 */     writeTag(2, 0);
/* 102 */     writeInt(segment.getCount());

/* 104 */     writeTag(3, 0);
/* 105 */     writeDouble(segment.getSum());

/* 107 */     writeTag(4, 0);
/* 108 */     writeDouble(segment.getAvg());

/* 110 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 115 */       writeVarint(value.getTime());
/* 116 */     } catch (IOException e) {
/* 117 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 123 */       writeVarint(Double.doubleToLongBits(value));
/* 124 */     } catch (IOException e) {
/* 125 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 131 */       writeVarint(value & 0xFFFFFFFFL);
/* 132 */     } catch (IOException e) {
/* 133 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 139 */       this.m_out.writeUTF(value);
/* 140 */     } catch (IOException e) {
/* 141 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 147 */       this.m_out.writeByte((field << 2) + type);
/* 148 */     } catch (IOException e) {
/* 149 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 155 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 156 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 159 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 160 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/business/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */